package com.jt.util;

import com.jt.pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserThreadLocal {
    //定义本地线程变量
    private static ThreadLocal<User>threadLocal = new ThreadLocal<>();
    //定义数据新增的方法
    public static void set(User user){
        threadLocal.set(user);
    }
    //获取数据
    public static User get(){
        return threadLocal.get();
    }
    //移除方法 使用ThreadLocal时切记将数据移除 否则极端条件下 容易产生内存溢出
    public static void remove(){
        threadLocal.remove();
    }
    //实现数据移除
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,Object handler,Exception exception){
        UserThreadLocal.remove();
    }
}
